import json


def parse_json(payload, required, mandatory=None):
    _payload = json.loads(payload)
    ret_dict = {}
    if required is None:
        raise KeyError
    else:
        for item in required:
            if item in _payload:
                ret_dict[item] = _payload[item]
            else:
                raise KeyError
    return ret_dict


if __name__ == "__main__":
    payload1 = '{"key1": "value1", "key2": "value2", "key3": "value3"}'
    payload2 = '{"key2": "value2", "key3": "value3"}'
    case1 = ["key1", "key2"]
    case2 = ["key1", "key2"]
    print(parse_json(payload1, case1))
    print(parse_json(payload2, case2))